package b_state.b_design_patten;

/**
 * Created by wug on 2016/6/14 0014.
 */
public class StateSold implements State {
    WaterMachine waterMachine;

    public StateSold(WaterMachine waterMachine) {
        this.waterMachine = waterMachine;
    }

    @Override
    public void workerPut() {
        waterMachine.setState(new StateSoldOut(waterMachine));
        waterMachine.put();
    }

    @Override
    public void insertMoney(int num) {
        waterMachine.setState(new StateNoMoney(waterMachine));
        waterMachine.insert(num);
    }

    @Override
    public void pressSellBtn() {
        if (waterMachine.getMoney() >= 5) {
            if (waterMachine.getNum()>0) {
                waterMachine.setMoney(waterMachine.getMoney()-5);
                waterMachine.setNum(waterMachine.getNum()-1);
                System.out.println("售水完毕!目前售水机中还剩" + waterMachine.getNum() + "瓶水");
                if (waterMachine.getMoney()>0) {
                    waterMachine.setState(new StateHaveMoney(waterMachine));
                } else {
                    waterMachine.setState(new StateNoMoney(waterMachine));
                }
            } else {
                waterMachine.setState(new StateSoldOut(waterMachine));
                System.out.println("对不起，水已售完！");
            }
        } else {
            System.out.println("钱不够5元买不了水！");
        }
    }

    @Override
    public void refundRocker() {
        waterMachine.setState(new StateHaveMoney(waterMachine));
        waterMachine.retun();
    }
}
